package jnzb.model.nzb;

import java.io.Serializable;

/**
 * Represents a single file segment
 * 
 * @author AA1
 * 
 */
public class Segment implements Comparable<Segment>, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// attributes
	private int bytes;
	private int number;

	// body
	private String messageID;

	// Extra data for us to indicate whats been downloaded
	private String downloadedSegmentFilename = null;
	private boolean downloadAttempted = false;

	public int getBytes() {
		return bytes;
	}

	public void setBytes(int bytes) {
		this.bytes = bytes;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getMessageID() {
		return messageID;
	}

	public void setMessageID(String messageID) {
		this.messageID = messageID;
	}

	public String getDownloadedSegmentFilename() {
		return downloadedSegmentFilename;
	}

	public void setDownloadedSegmentFilename(String downloadedSegmentFilename) {
		this.downloadedSegmentFilename = downloadedSegmentFilename;
	}

	public boolean hasBeenDownloaded() {
		return this.downloadAttempted && this.downloadedSegmentFilename != null;
	}

	public boolean downloadFailed() {
		return this.downloadAttempted && this.downloadedSegmentFilename == null;
	}

	public boolean isDownloadAttempted() {
		return downloadAttempted;
	}

	public void setDownloadAttempted(boolean downloadAttempted) {
		this.downloadAttempted = downloadAttempted;
	}

	/**
	 * Used to determine the order in the file by segment number
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Segment compareTo) {
		int compareSegNum = ((Segment) compareTo).getNumber();
		int compResult = 0;
		if (compareSegNum < this.getNumber()) {
			compResult = 1;
		} else if (compareSegNum > this.getNumber()) {
			compResult = -1;
		}
		// else they are equal, leave as 0
		return compResult;
	}
}
